<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="账号" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号类型" prop="accountType">
        <el-select v-model="form.accountType">
          <el-option
            v-for="dict in dict.type.seller_account_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>


    </el-form>
    <el-table
      v-loading="loading"
      :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="角色" align="center" prop="roleId">
        <template slot-scope="scope">
          <span>{{ getRoleName(scope.row.roleId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="roleId">
        <template slot-scope="scope">
          <span>{{ getStatusName(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['user_manager:modify']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleBan(scope.row)"
            v-hasPermi="['user_manager:ban']"
          >{{ getStatusOperatorName(scope.row.status) }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

    <!-- 发送邮件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送邮件" prop="mail">
              <el-input v-model="form.mail" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <div>
            <el-col :span="12">
            <el-form-item label="昵称" prop="nickname">
              <el-input v-model="form.nickname" placeholder="昵称" />
            </el-form-item>
          </el-col>
          </div>
          <div>
            <el-col :span="12">
            <el-form-item label="角色" prop="role">
              <el-select v-model="form.role" placeholder="请选择角色">
                <el-option
                  v-for="dict in dict.type.account_role"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          </div>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSendMail">发 送</el-button>
        <el-button @click="cancelSendMail">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改用户昵称和角色对话框 -->
    <el-dialog :title="title" :visible.sync="modifyUserOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item :label="form.mail" prop="mail">
              <el-div label="form.mail" /> -->
            <!-- </el-form-item> -->
          <!-- </el-col> -->
          <div>
            <el-col :span="12">
            <el-form-item label="昵称" prop="nickname">
              <el-input v-model="form.nickname" placeholder="昵称" />
            </el-form-item>
          </el-col>
          </div>
          <div>
            <el-col :span="12">
            <el-form-item label="角色" prop="role">
              <el-select v-model="form.role" placeholder="请选择角色">
                <el-option
                  v-for="dict in dict.type.account_role"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          </div>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitModifyUser">修 改</el-button>
        <el-button @click="cancelModifyUser">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, forceLogout } from "@/api/monitor/online";
import { sendMail, userManagerList, modifyUser, changeUserStatus } from "@/api/system/user";

export default {
  textAdmin:"超级管理员",
  textCommon:"管理员",

  name: "SellerList",
  dicts: ['seller_account_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,

      title: "",
      // 是否显示邮件发送的弹窗
      open: false,
      // 是否显示修改用户昵称和角色
      modifyUserOpen: false,
      // 表单参数
      form: {},
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,

      modifyUserForm: {},

      // 查询参数
      queryParams: {
        nickName: undefined
      },
      rules: {
        mail: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        role: [
          { required: true, message: "角色不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.form.accountType = "0"
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      userManagerList(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    // handleForceLogout(row) {
    //   this.$modal.confirm('是否确认强退名称为"' + row.userName + '"的用户？').then(function() {
    //     return forceLogout(row.tokenId);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("强退成功");
    //   }).catch(() => {});
    // },

    handleSendMail() {
      this.title = "发送邮件"
      this.form = {
        mail: undefined,
        nickname: undefined,
        role: undefined,
      };
      this.open = true
    },
    submitSendMail() {
      if (!this.form.mail || this.form.mail == "") {
        this.$modal.error("请输入正确的邮箱")
        return
      }
      if (!this.form.nickname || this.form.nickname == "") {
        this.$modal.error("请输入昵称")
        return
      }
      if (!this.form.role || this.form.role == "") {
        this.$modal.error("请输入角色")
        return
      }

      sendMail(this.form).then(response => {
        if (typeof response === "string") {
          this.$modal.error("发送失败:" + response)
        } else {
          this.$modal.msgSuccess("发送成功")
          this.open = false
        }
      });
    },
    cancelSendMail() {
      this.open = false
    },

    handleEdit(row) {
      this.title = "修改用户: " +  row.userName
      console.log(row)
      this.form = {
        mail: row.userName,
        nickname: row.nickName,
        role: undefined,
      };
      this.modifyUserOpen = true
    },
    
    submitModifyUser() {
      if (!this.form.mail || this.form.mail == "") {
        this.$modal.error("请输入正确的邮箱")
        return
      }
      if (!this.form.nickname || this.form.nickname == "") {
        this.$modal.error("请输入昵称")
        return
      }
      if (!this.form.role || this.form.role == "") {
        this.$modal.error("请输入角色")
        return
      }

      modifyUser(this.form).then(response => {
        if (typeof response === "string") {
          this.$modal.error("修改失败:" + response)
        } else {
          this.$modal.msgSuccess("修改成功")
          this.modifyUserOpen = false
          this.getList();
        }
      });
    },

    cancelModifyUser() {
      this.modifyUserOpen = false
    },

    getRoleName(roleId) {
      if (roleId == 100) {
        return "超级管理员"
      }
      if (roleId == 1) {
        return "ADMIN"
      } 
      if (roleId == 2) {
        return "管理员"
      }
      return "" + roleId
    },

    getStatusOperatorName(status) {
      return status === "0" ? "禁用" : "启用";
    },

    getStatusName(status) {
      return status === "0" ? "正常" : "已禁用";
    },

    handleBan(row) {
      let text = row.status === "0" ? "禁用" : "启用";
      let newStatus = row.status === "0" ? "1":"0";
      this.$modal.confirm(`是否${text}账号:[ ${row.userName} ]的用户？`).then(() => {
        return changeUserStatus(row.userId, newStatus);
        // return forceLogout(row.tokenId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(`${text}成功`);
      }).catch(() => {
        // row.status = row.status === "0" ? "1" : "0";
      });
    }
  }
};
</script>

